package org.sagovski.inf5040.peersim.reports;

import example.gossip.BasicShuffle;
import example.gossip.SendEntry;
import peersim.config.Configuration;
import peersim.core.Control;
import peersim.core.Network;
import peersim.graph.GraphFactory;
import peersim.reports.GraphObserver;
import peersim.util.IncrementalStats;
import peersim.vector.SingleValue;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SendEntryReportWriter extends GraphObserver implements Control, ReportWriter {
    private final String AVG_PATH_LENGTH_OUTPUT_REPORT_PATH = PathUtils.getAbsolutePath("SendEntryReport");

    // /////////////////////////////////////////////////////////////////////
    // Constructor
    // /////////////////////////////////////////////////////////////////////

    /**
     * Creates a new observer reading configuration parameters.
     */
    public SendEntryReportWriter(String name) {
        super(name);
        try {
            new File(AVG_PATH_LENGTH_OUTPUT_REPORT_PATH).delete();
            new File(AVG_PATH_LENGTH_OUTPUT_REPORT_PATH).createNewFile();
        } catch (IOException e) {
            System.err.println(ExceptionUtils.getStrStackTrace(e));
        }
    }

    // /////////////////////////////////////////////////////////////////////
    // Methods
    // /////////////////////////////////////////////////////////////////////

    /**
     * Print statistics for an average aggregation computation. Statistics
     * printed are defined by {@link IncrementalStats#toString}. The current
     * timestamp is also printed as a first field.
     */
    @Override
    public boolean execute() {
        long time = peersim.core.CommonState.getTime();
        updateGraph();
        int dis = 0;
        int sum =0 ;
        List<SendEntry> entries = new ArrayList<>();

        for (int i = 0; i < Network.size(); i++) {

            BasicShuffle protocol = (BasicShuffle) Network.get(i)
                    .getProtocol(pid);
            for (SendEntry entry : protocol.sendEntries) {
                ga.dist(g, entry.getFrom());
                if (ga.d[entry.getTo()] == -1) {
                    dis = -1;
                    break ;
                } else {
                    if (dis == -1){
                        continue;
                    }
                    dis += ga.d[entry.getTo()];
                }
                if (ga.d[entry.getTo()] == 0){
                    //System.out.println("entries" + entry);
                }

            }
            sum += protocol.sendEntries.size();
            entries.addAll(protocol.sendEntries);
            protocol.sendEntries = new ArrayList<>();
        }


        /* Printing statistics */
        System.out.println(name + ": " + time + " " + dis  + " " + sum + entries);
        this.writeEntry(time + " " + dis );

        /* Terminate if accuracy target is reached */
        return false;
    }

    @Override
    public void writeEntry(String reportEntry) {
        try {
            Files.write(Paths.get(AVG_PATH_LENGTH_OUTPUT_REPORT_PATH), (reportEntry + "\n").getBytes(),
                    StandardOpenOption.APPEND);
        } catch (IOException e) {
            System.err.println(ExceptionUtils.getStrStackTrace(e));
        }
    }
}
